﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Utilities;
using Xuanren_FrontEnd.Model;

namespace Xuanren_FrontEnd.Areas.Back.Controllers
{
    public class ProductController : Controller
    {
        private XuanrenTherapyEntities db = new XuanrenTherapyEntities();
        // GET: Product
        public ActionResult Index()
        {
 
            var list = db.Category.ToList();//获取到新闻分类
            return View(list);
        }
        [HttpPost]
        public ActionResult Search(int page, int limit, string name, string category_id)
        {

            var q = db.view_PruductAndCategory.Where(x => true);
            //这里实现多条件查询
            if (!string.IsNullOrWhiteSpace(name))
            {
                q = q.Where(x => x.name.Contains(name));//根据标题模糊查询
            }

            if (!string.IsNullOrWhiteSpace(category_id))
            {
                var Id = Convert.ToInt32(category_id);
                q = q.Where(x => x.category_id == Id);//根据类型查询
            }

            var count = q.Count();//得到总统计数
            //根据时间排序，并实现分页
            var list = q.OrderByDescending(x => x.pid).Skip((page - 1) * limit).Take(limit).Select(x => new
            {
                id = x.pid,
                name = x.name,
                brand = x.brand,
                image = x.image,
                price = x.price,
                info = x.info,
                create_time = x.create_time.ToString(),
                specification = x.specification,
                category_id = x.TypeName,
                model = x.model
            }).ToList();
            return Json(new { code = 0, msg = "", count = count, data = list });
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Add()
        {
             
            var list = db.Category.ToList();//获取到产品分类
            return View(list);
        }
        [HttpPost]
        public ActionResult Add(Product product)
        {
            {
                product.create_time = DateTime.Now;
                product.create_user = Session["username"].ToString();
                product.update_time = DateTime.Now;
                product.update_user = Session["username"].ToString();

                db.Product.Add(product);//添加
                int r = db.SaveChanges();
                if (r > 0)
                {
                    return Json(new { suc = true, msg = "添加成功" });
                }
                else
                {
                    return Json(new { suc = false, msg = "添加失败" });

                }
            }
        }
        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public ActionResult Update(int id)
        {
             
            ViewBag.Category = db.Category.ToList();//类型传值，可以将值传入到页面当中
            var obj = db.Product.SingleOrDefault(x => x.id == id);

            return View(obj);//强类型传值
        }
        [HttpPost]
        public ActionResult Update(Product product)
        {
             
            var obj = db.Product.SingleOrDefault(x => x.id == product.id);//获取指定Id的新闻对象
            obj.name = product.name;//产品名称

            obj.brand = product.brand;//品牌

            obj.info = product.info;

            obj.specification = product.specification;

            obj.price = product.price;
            obj.create_time = product.create_time;
            obj.category_id = product.category_id;
            obj.image = product.image;



            int r = db.SaveChanges();
            if (r > 0)
            {
                return Json(new { suc = true, msg = "修改成功" });
            }
            else
            {
                return Json(new { suc = false, msg = "修改失败" });

            }
        }

        [HttpGet]
        public ActionResult Create()
        {
 
            {
                var obj = db.Product.Where(x => true).ToList();
                DataTable dt = ExcelHelper.ExcelToDataTable(obj.ToString(), "商品表", true);//path路径
                ViewBag.A = dt;
            }

            return View();
        }

        [HttpPost]
        public ActionResult Create(Product p)
        {
            return View();
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public ActionResult Delete(int id)
        {
             
            var obj = db.Product.FirstOrDefault(x => x.id == id);
            if (obj != null)
            {
                db.Product.Remove(obj);//删除该新闻
            }
            int r = db.SaveChanges();
            if (r > 0)
            {
                return Json(new { scu = true, msg = "删除成功" });
            }
            else
            {
                return Json(new { scu = false, msg = "删除失败" });

            }
        }
        public ActionResult ListToExcel(string name, string category_id)
        {
 
            {//获取所有轮播图列表
                var list = db.Product.Where(p => true);
                if (!string.IsNullOrWhiteSpace(name))
                {
                    list = list.Where(x => x.name.Contains(name));//根据标题模糊查询
                }
                if (!string.IsNullOrWhiteSpace(category_id))
                {
                    var Id = Convert.ToInt32(category_id);
                    list = list.Where(x => x.category_id == Id);//根据类型查询
                }
                //把List转换成DataTable
                DataTable dt = Extensions.ToDataTable(list.ToList());
                //转换成二进制文件
                byte[] file = ExcelHelper.DataTableToExcel(dt, "用户信息", true);
                //导出
                return File(file, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");

            }
        }
    }
}